package com.taotao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taotao.mapper.*;
import com.taotao.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapperCust.CommonMapperCust;
import com.taotao.pojo.EyGoods;
import com.taotao.pojo.EyMember;
import com.taotao.pojo.EyService;
import com.taotao.pojo.EySessionInfo;
import com.taotao.pojo.EySessionInfoExample;

@Service
public class SessionInfoServiceImpl implements SessionInfoService{

	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	EyGoodsMapper eyGoodsMapper;
	@Autowired
	EyServiceMapper eyServiceMapper;
	@Autowired
	EySessionInfoMapper eySessionInfoMapper;
	@Autowired
	EyMessageInfoMapper eyMessageInfoMapper;
	@Autowired
	CommonMapperCust commonMapperCust;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	ParamService paramService;
	@Autowired
	PictureService pictureService;
	@Autowired
	CommonService commonService;
	
	BaseResult baseResult = new BaseResult();
	

	@Override
	public Map<String, Object> getSessionList(Map<String, String> map) {
		try {
			String pageNum=map.get("pageNum");
			String pageSize=map.get("pageSize");
			String memberId=map.get("memberId");
			String currentTab=map.get("currentTab"); //1表示查询用户会话，2表示查询机器人会话 3系统会话 4服务类会话   默认是1
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			}
			
			String adminMemberId = paramService.getParam("admin_member_id");
			String isAdmin= member.getIsAdmin();
			
			HashMap<String,String> m=new HashMap<>();
			
			m.put("sender",memberId);
			m.put("isAdmin",isAdmin);
			m.put("adminMemberId",adminMemberId);
			m.put("currentTab",currentTab);
			PageHelper.startPage(num,size);
			List<Map> list = commonMapperCust.selectSessionInfoList(m);
			if(list==null || list.size()==0){
				return baseResult.getSuccMap();
			}

			String tencentCosEnable=paramService.getParam(Constants.tencent_cos_enable);
			EyGoods goods;
			EyService service;
			for(Map<String,Object> info:list){
				String msgType=String.valueOf(info.get("msgType"));
				String updateTime=String.valueOf(info.get("updateTime"));
				String receiver=String.valueOf(info.get("receiver"));
				String sender=String.valueOf(info.get("sender"));
				String goodsId=String.valueOf(info.get("goodsId"));
				String serviceId=String.valueOf(info.get("serviceId"));
				String sessionKey=String.valueOf(info.get("sessionKey"));
				
				if("2".equals(msgType)){
					info.put("content", "[image]");
				}
				if("3".equals(msgType)){
					info.put("content", "[location]");
				}
				if("7".equals(msgType)){
					info.put("content", "[transfer]");
				}
				
				String language = paramService.getParam("language");
				updateTime=DateUtil.fromToday(DateUtil.strToDate(updateTime), language);
				info.put("updateTime", updateTime);

				EyMember  receiverMember= eyMemberMapper.selectByPrimaryKey(Integer.parseInt(receiver));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(receiverMember.getImageUrl())){
					receiverMember.setAvatarUrl(receiverMember.getImageUrl());
				}
				info.put("receiver",receiverMember);
				
				EyMember  senderMember= eyMemberMapper.selectByPrimaryKey(Integer.parseInt(sender));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(senderMember.getImageUrl())){
					senderMember.setAvatarUrl(senderMember.getImageUrl());
				}
				info.put("sender",senderMember);
				
				if(StringUtils.isNotBlank(goodsId) && !"null".equalsIgnoreCase(goodsId)){
					goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
					String imageUrl = goods.getImageUrl();
					if(Constants.false_string.equals(tencentCosEnable)) {
						imageUrl=pictureService.changeDomain(imageUrl);
					}
					goods.setImageUrl(imageUrl);
					info.put("goods", goods);
				}
				
				if(StringUtils.isNotBlank(serviceId) && !"null".equalsIgnoreCase(serviceId)){
					service=eyServiceMapper.selectByPrimaryKey(Integer.parseInt(serviceId));
					String imageUrl = service.getImageUrl();
					if(Constants.false_string.equals(tencentCosEnable)) {
						imageUrl=pictureService.changeDomain(imageUrl);
					}
					service.setImageUrl(imageUrl);
					info.put("service", service);
				}
				
				String isRead=commonMapperCust.getIsRead(sessionKey);
				info.put("isRead", isRead);
			}
			
			PageInfo<Map> pageInfo = new PageInfo<>(list);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	
	@Override
	public Map<String, Object> querySessionList(Map<String, String> map) {
		try {
			String pageNum=map.get("pageNum");
			String pageSize=map.get("pageSize");
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			
			PageHelper.startPage(num,size);
			List<Map> list = commonMapperCust.selectSessionInfoList2(map);

			for(Map info:list){
				String updateTime=String.valueOf(info.get("updateTime"));
				String receiver=String.valueOf(info.get("receiver"));
				String sender=String.valueOf(info.get("sender"));
				String isRead=String.valueOf(info.get("isRead"));
				
				info.put("updateTime", DateUtil.getDateTimeFormatStr(updateTime));
				
				EyMember  receiverMember= eyMemberMapper.selectByPrimaryKey(Integer.parseInt(receiver));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(receiverMember.getImageUrl())){
					receiverMember.setAvatarUrl(receiverMember.getImageUrl());
				}
				info.put("receiver",receiverMember);
				
				String receiverUnionId=receiverMember.getUnionId();
				if(StringUtils.isNotBlank(receiverUnionId)){
					Map<String, String> receiverSubscribe = commonService.querySubscribe(receiverUnionId);
					info.put("receiverSubscribe",receiverSubscribe);
				}
				
				EyMember  senderMember= eyMemberMapper.selectByPrimaryKey(Integer.parseInt(sender));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(senderMember.getImageUrl())){
					senderMember.setAvatarUrl(senderMember.getImageUrl());
				}
				info.put("sender",senderMember);
				
				String senderUnionId=senderMember.getUnionId();
				if(StringUtils.isNotBlank(senderUnionId)){
					Map<String, String> senderSubscribe = commonService.querySubscribe(senderMember.getUnionId());
					info.put("senderSubscribe",senderSubscribe);
				}
				
				info.put("isRead", "1".equals(isRead)?Constants.isReadYes:Constants.isReadNo);
			}
			
			PageInfo<Map> pageInfo = new PageInfo<>(list);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	
	@Override
	public Map<String, Object> deleteSessionInfo(Map<String, String> map) {
		try {
			String sessionKey=map.get("sessionKey");
			String memberId=map.get("memberId");
			EySessionInfoExample example = new EySessionInfoExample();
			com.taotao.pojo.EySessionInfoExample.Criteria criteria = example.createCriteria();
			criteria.andSessionKeyEqualTo(sessionKey);
			criteria.andSenderEqualTo(Integer.parseInt(memberId));
			List<EySessionInfo> sessionList = eySessionInfoMapper.selectByExample(example);
			if(sessionList!=null && sessionList.size()>0){
				EySessionInfo sessionInfo = sessionList.get(0);
				sessionInfo.setStatus(Constants.num_100);
				eySessionInfoMapper.updateByPrimaryKey(sessionInfo);
			}
	  	    
			operateLogService.insert(Integer.parseInt(memberId), null,null,"删除会话",OperateType.operateType_12.getCode(),sessionKey); 

			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}



	@Override
	public Map<String, Object> isReadSession(Map<String, String> map) {
		try {
			
			String memberId=map.get("memberId");
			String sessionKey=map.get("sessionKey");
			String isRead=map.get("isRead");
			
			EySessionInfoExample example = new EySessionInfoExample();
			com.taotao.pojo.EySessionInfoExample.Criteria criteria = example.createCriteria();
			criteria.andSessionKeyEqualTo(sessionKey);
			criteria.andReceiverEqualTo(Integer.parseInt(memberId));
			
			EySessionInfo sessionInfo=new EySessionInfo();
			sessionInfo.setIsRead(isRead); 
			
			eySessionInfoMapper.updateByExampleSelective(sessionInfo, example);
	  	    
	  	  return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	@Override
	public Map<String, Object> getSessionKey(Map<String, String> map) {
		try {
			String sender=map.get("sender");
			String receiver=map.get("receiver");
			String goodsId=map.get("goodsId");
			String serviceId=map.get("serviceId");
			
			EySessionInfoExample example = new EySessionInfoExample();
			com.taotao.pojo.EySessionInfoExample.Criteria criteria = example.createCriteria();
			criteria.andSenderEqualTo(Integer.parseInt(sender));
			criteria.andReceiverEqualTo(Integer.parseInt(receiver));
			if(StringUtils.isNotBlank(goodsId)){
				criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
			}
			if(StringUtils.isNotBlank(serviceId)){
				criteria.andServiceIdEqualTo(Integer.parseInt(serviceId));
			}
			criteria.andStatusNotEqualTo(Constants.num_100); //会话删除的排除
			List<EySessionInfo> list = eySessionInfoMapper.selectByExample(example);
			String sessionKey="";
			if(list!=null && list.size()>0){
				EySessionInfo sessionInfo=list.get(0);
				sessionKey=sessionInfo.getSessionKey();
			}
			return baseResult.getSuccMap(sessionKey);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
}
